﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CoolERP_Common;
using CoolERP_BLL;
using CoolERP_Entities;
using System.Collections;
using System.IO;

namespace CoolERP_Web.User
{
    public partial class KC_PD_WL : BasePage
    {
        static Hashtable htPD = new Hashtable();
        static Hashtable htCY = new Hashtable();//差异（盘点多出的物料）
        static List<WLKCModel> zcLists = new List<WLKCModel>();//正常
        static List<WLKCModel> pyLists = new List<WLKCModel>();//盘盈
        static List<WLKCModel> pkLists = new List<WLKCModel>();//盘亏
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void btnImport_Click(object sender, ImageClickEventArgs e)
        {
            htPD.Clear();
            htCY.Clear();
            zcLists.Clear();
            pyLists.Clear();
            pkLists.Clear();

            try
            {
                if (string.IsNullOrEmpty(txtDataList.Text.Trim()))
                    this.labMsg.Text = "提示：请导入盘点数据";
                else
                {
                    string[] dataList = txtDataList.Text.Trim().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                    Hashtable ht = new Hashtable();
                    foreach (var item in dataList)
                    {
                        if (!string.IsNullOrEmpty(item))
                        {
                            string[] tempString = item.Split(new string[] { "," }, StringSplitOptions.None);
                            string key = tempString[0];//编号+仓位
                            decimal sl = Convert.ToDecimal(tempString[1]);//数量
                            if (ht.ContainsKey(key))
                                ht[key] = Convert.ToDecimal(ht[key]) + sl;
                            else
                                ht[key] = sl;
                        }
                    }
                    htPD = ht.Clone() as Hashtable;
                    BindData(ht);
                    labMsg.Text = "";
                    string filePath = string.Format("{0}WL{1}.txt", Server.MapPath(Utility.InventoryPath), DateTime.Now.ToString("yyyyMMddHHmmss"));
                    File.WriteAllText(filePath, txtDataList.Text.Trim());
                }
            }
            catch (Exception ex)
            {
                this.labMsg.Text = "提示：导入的数据有误";
            }
        }

        private void BindData(Hashtable ht)
        {
            int RecordCount = 0;
            using (KCBLL kBLL = new KCBLL())
            {
                List<WLKCModel> itemLists = kBLL.GetWLKC(null, false, "BH asc", null, 10000, out RecordCount);//所有物料库存
                foreach (var key in ht.Keys)
                {
                    string bh = key.ToString().Split('/')[0];
                    string cw = key.ToString().Split('/')[1];
                    WLKCModel item = itemLists.FirstOrDefault(t => t.BH.Trim() == bh && t.CWMC == cw);
                    if (item != null)
                    {
                        double sl = Convert.ToDouble(ht[key]);
                        if (item.SL == sl)
                            zcLists.Add(item);
                        else if (item.SL > sl)
                            pkLists.Add(item);
                        else
                            pyLists.Add(item);
                    }
                    else
                        htCY[key] = ht[key];
                }
                zcLists = zcLists.OrderBy(t => t.BH).ToList();
                pkLists = pkLists.OrderBy(t => t.BH).ToList();
                pyLists = pyLists.OrderBy(t => t.BH).ToList();
                GridView1.DataSource = zcLists;
                GridView1.DataBind();
                panStat.Visible = true;
                liKcTotal.Text = itemLists.Count.ToString();
                liPdTotal.Text = ht.Keys.Count.ToString(); 
                liWl.Text = ht.Keys.Count.ToString();
                liZC.Text = zcLists.Count.ToString();
                liPY.Text = pyLists.Count.ToString();
                liPK.Text = pkLists.Count.ToString();
                List<int> cols = new List<int>() { 1, 2, 3, 4, 5, 6, 7 };
                Utility.MergeCols(GridView1, cols);
            }
        }

        protected void LinkButton_Command(object sender, CommandEventArgs e)
        {
            if (htPD.Keys.Count > 0)
            {
                switch (e.CommandArgument.ToString())
                {
                    case "0":
                        GridView1.DataSource = zcLists;
                        GridView1.DataBind();
                        break;
                    case "1":
                        GridView1.DataSource = pyLists;
                        GridView1.DataBind();
                        break;
                    case "2":
                        GridView1.DataSource = pkLists;
                        GridView1.DataBind();
                        break;
                }                
            }
            else
                this.ShowMessage("请先导入数据");
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label labPDZL = e.Row.FindControl("labPDZL") as Label;//盘点总量
                WLKCModel wl = e.Row.DataItem as WLKCModel;
                string key = wl.BH.Trim() + "/" + wl.CWMC;
                labPDZL.Text = Convert.ToDouble(htPD[key]).ToString("F");
            }
        }
    }
}